//
//  GCDAsyncUdpSocketManager.m
//  GCDAsyncSocketManagerDemo
//
//  Created by 于鹏 on 2017/7/13.
//  Copyright © 2017年 于鹏. All rights reserved.
//

#import "GCDAsyncUdpSocketManager.h"
#import "GCDAsyncUdpSocket.h"

#define udpPort 2012
@interface GCDAsyncUdpSocketManager ()<GCDAsyncUdpSocketDelegate>
@property (nonatomic, strong) GCDAsyncUdpSocket *udpSocket;
@property (nonatomic, assign) uint16_t port;  // Sokcet连接的port

@end

@implementation GCDAsyncUdpSocketManager

+ (GCDAsyncUdpSocketManager *)udpShareInstance {
    static GCDAsyncUdpSocketManager *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc]init];
    });
    return instance;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        self.connectStatus = -1;
    }
    return self;
}

#pragma mark - socket actions
//- (void)changePort:(NSInteger)port {
//    self.port = port;
//}
//- (void)connectUdpSocketWithDelegate:(id)delegate {
//    if (self.connectStatus != -1) {
////        XLLog(@"UDP Socket Connect: YES");
//        return;
//    }
//    
//    self.connectStatus = 0;
//    
//    _udpSocket = [[GCDAsyncUdpSocket alloc]initWithDelegate:delegate delegateQueue:dispatch_get_main_queue()];
//    NSError * error = nil;
//    [_udpSocket bindToPort:self.port error:&error];
//    if (error) {
//        self.connectStatus = -1;
////        XLLog(@"UDP connect error: --- %@",error);
//    }else {
//        self.connectStatus = 1;
//        [_udpSocket beginReceiving:&error];
////        XLLog(@"UDP connnect success");
//    }
//
//}

- (void)connectUdpSocket {
    if (self.connectStatus != -1) {
//        XLLog(@"UDP Socket Connect: YES");
        return;
    }
    
    self.connectStatus = 0;
    
    _udpSocket = [[GCDAsyncUdpSocket alloc]initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
    NSError * error = nil;
    [_udpSocket bindToPort:udpPort error:&error];
    if (error) {
        self.connectStatus = -1;
//        XLLog(@"UDP connect error: --- %@",error);
    }else {
        self.connectStatus = 1;
        [_udpSocket beginReceiving:&error];
//        XLLog(@"UDP connnect success");
    }
}

- (void)updSocketSendData:(NSData *)sendData toAddress:(NSData *)address {
    [self.udpSocket sendData:sendData toAddress:address withTimeout:-1 tag:0];
}

#pragma mark - GCDAsyncUdpSocketDelegate

- (void)udpSocket:(GCDAsyncUdpSocket *)sock didSendDataWithTag:(long)tag
{
    //    XLLog(@"UDP发送信息成功");
}

- (void)udpSocket:(GCDAsyncUdpSocket *)sock didNotSendDataWithTag:(long)tag dueToError:(NSError *)error
{
//    XLLog(@"UDP发送信息失败:%@",error);
}

//udp接收数据
- (void)udpSocket:(GCDAsyncUdpSocket *)sock didReceiveData:(NSData *)data fromAddress:(NSData *)address withFilterContext:(id)filterContext
{
    if ([self.udpSocketDelegate respondsToSelector:@selector(udpSocketReceiveData:fromAddress:)]) {
        [self.udpSocketDelegate udpSocketReceiveData:data fromAddress:address];
    }
}
- (void)udpSocketDidClose:(GCDAsyncUdpSocket *)sock withError:(NSError *)error
{
//    XLLog(@"udpSocket关闭:%@",error);
//    self.connectStatus = -1;
}
@end
